package com.global.variable.common.web.webconfig;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.global.variable.common.web.interceptor.SecurityInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import java.util.List;


/***
* @methodname
* @Description {WEB 初始化相关配置}
* @author 王镇
* @create 2018/9/29 11:48
* @param
* @throws {如果有异常说明请填写}
* @return
*/
@Configuration
@ConditionalOnClass(value = {WebMvcConfigurationSupport.class})
@ConditionalOnBean(SecurityInterceptor.class)
public class WebConfig extends WebMvcConfigurationSupport {

    private static final Logger logger = LoggerFactory.getLogger(WebConfig.class);

    @Autowired
    private SecurityInterceptor securityInterceptor;

    /***
    * @methodname configureMessageConverters
    * @Description {默认静态资源处理器}
    * @author 王镇
    * @create 2018/9/29 11:56
    * @param converters
    * @throws {如果有异常说明请填写}
    * @return void
    */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        ObjectMapper objectMapper = builder.build();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        objectMapper.configure(MapperFeature.PROPAGATE_TRANSIENT_MARKER, true);// 忽略 transient 修饰的属性
        converters.add(new MappingJackson2HttpMessageConverter(objectMapper));
        super.configureMessageConverters(converters);
    }

    /**
     * 默认地址跳转到登录页
     * @param registry
     */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/system/to_login");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
        super.addViewControllers(registry);
    }

    /**
     * 添加静态资源访问处理
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
/*        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
        //registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");//*/
        //即：/webjars/** 映射到 classpath:/META-INF/resources/webjars/

        // H+前端项目项目
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        // keen前端项目路径
        registry.addResourceHandler("/webjar/**")
                .addResourceLocations("classpath:/META-INF/resources/webjar/");
        //新增 resourceChain 配置即开启缓存配置。
        //不知道为何不加这个配置 设置了 webjars-locator 未生效。。没过多纠结。。
    }

    /***
    * @methodname addInterceptors
    * @Description {添加拦截器}
    * @author 王镇
    * @create 2018/9/29 11:58
    * @param registry
    * @throws {如果有异常说明请填写}
    * @return void
    */
    @Override
    public void addInterceptors (InterceptorRegistry registry){
        InterceptorRegistration addInterceptor = registry.addInterceptor(securityInterceptor);
        // 排除配置
        addInterceptor.excludePathPatterns("/error");
        addInterceptor.excludePathPatterns("/static/**");//排除静态资源
        addInterceptor.excludePathPatterns("/system/to_login");
        addInterceptor.excludePathPatterns("/system/do_login");
        // 拦截配置
        addInterceptor.addPathPatterns("/**");




        //registry.addInterceptor(new SecurityInterceptor()).addPathPatterns("/**").excludePathPatterns("/system/to_login","/system/do_login","/static/**");
    }



}
